package com.guoxiang.bigdata.scheduler.impl;

import com.guoxiang.bigdata.scheduler.Scheduler;
import com.guoxiang.bigdata.util.JedisUtil;
import redis.clients.jedis.Jedis;

public class BaiduTiebaSchedualForRedis implements Scheduler {
    public static final String CURRENTPAGENUM = "currentPageNum";
    public static final String PAGELIST = "pageList";

    private Long currentPageNum = 0L;
    private static BaiduTiebaSchedualForRedis bdSchedual = new BaiduTiebaSchedualForRedis();
    public static BaiduTiebaSchedualForRedis getBdQueue() {
        return bdSchedual;
    }
    private BaiduTiebaSchedualForRedis(){

    }

    @Override
    public void push(String str) {
        Jedis conn= JedisUtil.getConn();
        conn.lpush(BaiduTiebaSchedualForRedis.PAGELIST,str);
        conn.close();

    }

    @Override
    public String pop() {
        Jedis conn = JedisUtil.getConn();
        String pageUrl = conn.rpop(BaiduTiebaSchedualForRedis.PAGELIST);
        return pageUrl;
    }
    public Long getCurrentPageNum() {
        Jedis conn = JedisUtil.getConn();
        String curr = conn.get(BaiduTiebaSchedualForRedis.CURRENTPAGENUM);
        if (curr == null) {
            return currentPageNum;
        }
        conn.close();
        return Long.valueOf(curr);
    }
    /**
     * 下一页
     */
    public void nextPage() {
        currentPageNum += 50;
        Jedis conn = JedisUtil.getConn();
        conn.set(BaiduTiebaSchedualForRedis.CURRENTPAGENUM, String.valueOf(currentPageNum));
        conn.close();
    }
}
